package cn.HuanG.pet.service.impl;

import cn.HuanG.base.service.impl.BaseServiceImpl;
import cn.HuanG.org.domain.Employee;
import cn.HuanG.org.domain.Shop;
import cn.HuanG.org.mapper.ShopMapper;
import cn.HuanG.pet.domain.SearchMasterMsg;
import cn.HuanG.pet.mapper.SearchMasterMsgMapper;
import cn.HuanG.pet.query.SearchMasterMsgQuery;
import cn.HuanG.pet.service.ISearchMasterMsgService;
import cn.HuanG.user.domain.User;
import cn.HuanG.user.mapper.UserMapper;
import cn.HuanG.util.DistanceUtil;
import cn.HuanG.util.LoginContext;
import cn.HuanG.util.PageInfo;
import cn.HuanG.util.Point;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Override
    public void publish(SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        /*
            需要设置参数：
            state；
            user_id
            shop_id
         */
        //设置状态
        searchMasterMsg.setState(0);
//        //获取user_id,首先就要通过request获取消息头,然后去redis获取数据
//        String token = request.getHeader("TOKEN");
//        System.out.println(token);
//        //token在redis中是key，logininfo是v
//        LoginInfo loginInfo = (LoginInfo)redisTemplate.opsForValue().get(token);
//        User user = null;
//        if (loginInfo.getType()==1){
//           user =  userMapper.loadByLoginInfoId(loginInfo.getId());
//            System.out.println(user);
//        }
        User user = (User)LoginContext.getCurrent(request);
        System.out.println("啊哈哈哈哈哈"+user);
        searchMasterMsg.setUser_id(user.getId());
        System.out.println("宠物"+searchMasterMsg);
             /*
            1.把用户地址解析 票point
            2.调用工具类 计算最近的门店
         */
        // 1.把用户地址解析 票point
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        // 2.查询全部门店，调用方法找到距离最近的门店
        List<Shop> shops = shopMapper.queryAll();
        Shop shop = DistanceUtil.getNearestShop(point, shops);
        //设置最近的门店
        searchMasterMsg.setShop_id(shop.getId());
        System.out.println("商铺"+searchMasterMsg);
        //保存数据
        super.save(searchMasterMsg);
    }

    @Override
    public PageInfo<SearchMasterMsg> queryPage(SearchMasterMsgQuery query, HttpServletRequest request) {

        //获取employee对象
        Employee employee = (Employee)LoginContext.getCurrent(request);
        System.out.println("嘻嘻嘻"+employee);
        //获取employee表中的shop_id，判断是否为空，为空就是管理员，能够查询全部，不为空就是店员
        if(employee.getShop_id()!=null){
            System.out.println("原灌灌灌灌"+employee.getShop_id());
        //通过shop_id为条件去查询t_search_master_msg表的数据
            query.setShopId(employee.getShop_id());
        }
        Long count = searchMasterMsgMapper.count(query);
        List<SearchMasterMsg> list = searchMasterMsgMapper.queryDate(query);
        return new PageInfo<>(count, list);
    }
}
